﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;

namespace com.tiyma.common
{
    public class AppConfig
    {
        /// <summary>
        /// sql连接字符串
        /// </summary>
        public static string SqlConnectionString
        {
            get { return GetConfig("SqlConnectionString", @"Data Source=.;Initial Catalog=Book;User ID=sa;Password=123456"); }
        }

        /// <summary>
        /// 索引文件路径根目录
        /// </summary>
        public static string BookIndexPath
        { 
            get 
            {
                string t_book_index = GetConfig("bookIndexPath", "d://bookIndex");
                if (string.IsNullOrEmpty(t_book_index))
                    t_book_index = "d://bookIndex";
                return t_book_index;
            } 
        }

        /// <summary>
        /// 索引文件路径根目录
        /// </summary>
        public static string ChapterPath
        {
            get
            {
                string t_chapter_path = GetConfig("ChapterPath", "D:\\work\\my_project\\juwenxue\\Site\\tiyma-1\\chapter\\{0}\txt\\{1}.txt");
                if (string.IsNullOrEmpty(t_chapter_path))
                    t_chapter_path = "D:\\work\\my_project\\juwenxue\\Site\\tiyma-1\\chapter\\{0}\txt\\{1}.txt";
                return t_chapter_path;
            }
        }

        /// <summary>
        /// 索引文件路径根目录
        /// </summary>
        public static string ChapterRoot
        {
            get
            {
                string t_chapter_root = GetConfig("ChapterRoot", "D:\\work\\my_project\\juwenxue\\Site\\tiyma-1\\chapter");
                if (string.IsNullOrEmpty(t_chapter_root))
                    t_chapter_root = "D:\\work\\my_project\\juwenxue\\Site\\tiyma-1\\chapter";
                return t_chapter_root;
            }
        }

        public static string Domain
        {
            get
            {
                return GetConfig("Domain", "tiyma.com");
            }
        }

        public static string UrlKey
        {
            get
            {
                return GetConfig("UrlKey", "tiyma.com");
            }
        }

       
        /// <summary>
        /// 是否从webservice获取数据
        /// </summary>
        public static bool WebService
        {
            get
            {
                bool t_ret = false;
                if (GetConfig("WebService", "") == "true")
                    t_ret = true;

                return t_ret;
            }
        }

        /// <summary>
        /// webservice地址
        /// </summary>
        public static string WebServiceUrl
        {
            get
            {
                return GetConfig("WebServiceUrl", "");
            }
        }

        /// <summary>
        /// MySQL数据库链接
        /// </summary>
        public static string MySQLConnectionString
        {
            get { return GetConfig("MySQLConnectionString", @"server=127.0.0.1;uid=root; Port=3306;pwd=123456;ConnectionTimeout=60; database=tiyma; pooling=false;"); }
        }

        /// <summary>
        /// 数据库名字
        /// </summary>
        public static string DbName
        {
            get { return GetConfig("DbName", @"tiyma"); }
        }

        public static string ICONPath
        {
            get
            {
                string t_icon_path = GetConfig("ICONPath", "d://bookIcons");
                if (string.IsNullOrEmpty(t_icon_path))
                    t_icon_path = "d://bookIndex";
                if (!System.IO.Directory.Exists(t_icon_path))
                    System.IO.Directory.CreateDirectory(t_icon_path);
                return t_icon_path;
            }
        }



        #region tools
        private static NameValueCollection _settings = null;

        public static NameValueCollection Settings
        {
            get { return AppConfig._settings; }
            set { AppConfig._settings = value; }
        }



        private static string GetConfig(string key, string defaultValue)
        {

            if (_settings == null)
                _settings = ConfigurationManager.AppSettings;

            if (Settings[key] != null)
                return Settings[key];
            else
                return defaultValue;

        }
        #endregion
    }
}
